#define _GNU_SOURCE
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

// 分线程函数
void *first_thread(void *arg)
{
    // char *data = (char *)arg;
    printf("first线程(%d)开始执行, arg=%s \n", gettid(), (char *)arg);
    
    return "atguigu";
}

void *second_thread(void *arg)
{
    // int val = *(int *)arg;
    printf("second线程(%d)开始执行, arg=%d\n", gettid(), *(int *)arg);

    return (void *)12;
}

// 主线程函数
int main(int argc, char const *argv[])
{
    printf("main线程(%d)开始执行 \n", gettid());

    // 创建2个分线程
    pthread_t first_pt, second_pt;
    pthread_create(&first_pt, NULL, first_thread, "abc");
    int val = 100;
    pthread_create(&second_pt, NULL, second_thread, &val);

    printf("main线程(%d)继续执行 first_pt=%ld, second_pt=%ld \n", 
        gettid(), first_pt, second_pt);

    // 等待分线程执行完毕
    char *first_result;
    int second_result;
    pthread_join(first_pt, (void **)&first_result);
    pthread_join(second_pt, (void **)&second_result);
    printf("first_result=%s, second_result=%d \n", first_result, second_result);

    printf("main线程(%d)结束 \n", gettid());

    return 0;
}
